package main

// https://www.jianshu.com/p/a667ba285ecb
// % go get golang.org/x/sync/errgroup
import (
	"errors"
	"fmt"
	"time"

	"golang.org/x/sync/errgroup"
)

func main() {

	var g errgroup.Group

	g.Go(func() error {
		fmt.Println("No.1 g.Go error start")
		time.Sleep(2 * time.Second)
		fmt.Println("No.1 g.Go error finish ")
		return nil
	})

	g.Go(func() error {
		fmt.Println("No.2 g.Go error start")
		time.Sleep(4 * time.Second)
		fmt.Println("No.2 g.Go error finish ")
		return errors.New("No.2 failed to execute")
	})

	g.Go(func() error {
		fmt.Println("No.3 g.Go error start")
		time.Sleep(6 * time.Second)
		fmt.Println("No.3 g.Go error finish")

		return nil
	})

	if err := g.Wait(); err == nil {
		fmt.Println("Successfully execute all")
	} else {
		fmt.Println("Failed:", err)
	}

}
